﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using VanTaiBLL;
using VanTaiData;
using VanTaiProperty;
using Roles;
using System.Web.UI.HtmlControls;

namespace VanTaiTMS.Page
{
    public partial class nhat_ky_hang_hoa : System.Web.UI.Page
    {
        #region[Define]
        private DataTable dtDO;
        private DataTable dtDOLine;
        private DataTable dtSearch;
        protected string SuggestionList;
        protected string StrAlert = "";
        private DataTable dtAutoComplete;
        private clsTMDOBusiness clsDO = new clsTMDOBusiness();
        private ClsTmUnitBusiness clsUnit = new ClsTmUnitBusiness();
        private TMDoNew_BusinessLogic clsDoNew = new TMDoNew_BusinessLogic();
        #endregion

        #region[Controller]
        protected void Page_Load(object sender, EventArgs e)
        {
            Autocomplate();
            if (!IsPostBack)
            {
                BindGridDO();
            }
        }
        protected void cmdSerach_Click(object sender, EventArgs e)
        {
            if (txtSearch.Text == "")
            {
                StrAlert += "<div class='alert alert-warning'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Vui lòng nhập vào từ khoá để tìm kiếm !";
                StrAlert += "</div>";
            }
            else
            {
                Search(txtSearch.Text);
            }
        }
        protected void cmdRefesh_Click(object sender, EventArgs e)
        {
            txtSearch.Text = "";
            BindGridDO();
        }
        //-------------------List-View-----------------------
        protected void ListViewAll_ItemCommand(object sender, ListViewCommandEventArgs e)
        {
            try
            {
                HttpCookie cookie = Request.Cookies["UserLogin"];
                bool Edit = SecurityServices.HasPermisstion(StringContent.QuanLyHangHoa, int.Parse(cookie["RolesID"]), TypeaAudit.Edit);
                if (e.CommandName == "Accept")
                {
                    if (Edit == false)
                    {
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridDoLine(SenderId);
                    }
                    else
                    {
                        string SenderId = ((HtmlInputHidden)e.Item.FindControl("hiddenId")).Value;
                        BindGridDoLine(SenderId);
                    }
                }
            }
            catch (Exception ex)
            {
                StrAlert = ex.ToString();            
            }
        }
        protected void ListViewAll_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LinkButton linkListName = (LinkButton)e.Item.FindControl("linkListName");
                LinkButton linkListName_E = (LinkButton)e.Item.FindControl("linkListName_E");
                string _jsDouble1 = ClientScript.GetPostBackClientHyperlink(linkListName, "");
            }
        }
        //-------------------Data-Pagger---------------------
        protected void DataPagerListAll_PreRender(object sender, EventArgs e)
        {
            DataPagerListAll.PageSize = 5;
            BindGridDO();
        }
        #endregion

        #region[Function]
        protected void BindGridDO()
        {
            dtDO = clsDoNew.Search(txtSearch.Text);
            if (dtDO != null && dtDO.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtDO;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
            }
        }
        protected void BindGridDoLine(string id)
        {
            dtDOLine = clsDoNew.GetDataDetailLineLog(id);
            if (dtDOLine != null && dtDOLine.Rows.Count > 0)
            {
                ListViewDetail.DataSource = dtDOLine;
                ListViewDetail.DataBind();
                ListViewDetail.Visible = true;
            }
            else
            {
                ListViewDetail.Visible = false;
            }
        }
        //AutoComplate
        protected void Autocomplate()
        {
            dtAutoComplete = clsDoNew.GetData();
            if (dtAutoComplete != null && dtAutoComplete.Rows.Count > 0)
            {
                foreach (DataRow dr in dtAutoComplete.Rows)
                {
                    if (string.IsNullOrEmpty(SuggestionList))
                    {
                        SuggestionList += "\"" + dr["No_"].ToString().Replace("\n", " ") + "\",";
                    }
                    else
                    {
                        SuggestionList += "\"" + dr["No_"].ToString().Replace("\n", " ") + "\",";
                    }
                }
            }
        }
        protected void Search(string keywords)
        {
            dtSearch = clsDoNew.Search(keywords);
            if (dtSearch != null && dtSearch.Rows.Count > 0)
            {
                ListViewAll.DataSource = dtSearch;
                ListViewAll.DataBind();
                ListViewAll.Visible = true;
                DataPagerListAll.Visible = true;

                StrAlert += "<div class='alert alert-success'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Tìm thấy " + dtSearch.Rows.Count.ToString() + " mẫu tin !";
                StrAlert += "</div>";
            }
            else
            {
                ListViewAll.Visible = false;
                DataPagerListAll.Visible = false;
                StrAlert += "<div class='alert alert-error'>";
                StrAlert += "<a href='#' class='close' data-dismiss='alert'>&times;</a>";
                StrAlert += "Không tìm thấy mẫu tin nào !";
                StrAlert += "</div>";
            }
        }
        #endregion

        #region[OpenDialog]
        private void OpenDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('open');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        private void CloseDialog(string dialogId)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("$('" + dialogId + "').dialog('close');");
            ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(),
               "scriptKey", sb.ToString(), true);
        }
        #endregion
    }
}